// pages/me/pages/login/login.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focus: false,
    pwdFocus:false,
    inputTelValue: '',
    inputPwdValue: '',
    timeChuo:"",
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    showCancel:false
  },
  staticData:{},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  setDisabled: function (e) {
    this.setData({
      disabled: !this.data.disabled
    })
  },
  setPlain: function (e) {
    this.setData({
      plain: !this.data.plain
    })
  },
  setLoading: function (e) {
    this.setData({
      loading: !this.data.loading
    })
  },
  bindButtonTap: function () {
    this.setData({
      focus:true,
      pwdFocus:false,
    })
  },
  bindButtonPwdTap: function () {
    this.setData({
      focus: false,
      pwdFocus:true,
    })
  },
  bindKeyPwdInput: function (e) {
    this.setData({
      inputPwdValue: e.detail.value
    })
  },

  inputPhoneNum:function(e){
    this.setData({
        inputTelValue: e.detail.value,
        showCancel:true,
        opacity:1
    })
  },
  // 手机号处取消
  bindCancelbtn:function(e){
    console.log(e)
    this.setData({
      inputTelValue: '',
      showCancel: false,
      opacity: 0.6
    })
  },
  bindButtonSubmit:function(){
    if (!this.data.inputTelValue) {
      wx.showToast({
        title: '请输入手机号',
        icon: "none",
        duration: 2000
      })
      return;
    }else{
      var reg = /^1[0-9]{10}$/;
      var flag = reg.test(this.data.inputTelValue); 
      if(flag==false){
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none",
          duration: 2000
        })
        return;
      }
    }
    if (!this.data.inputPwdValue) {
      wx.showToast({
        title: '请输入密码',
        icon: "none",
        duration: 2000
      })
      return;
    }
    this.handleSubmit();
  },
  handleSubmit(){
    var that=this;
    var myDate = new Date();
    var timestamp = new Date().getTime();
    var md5=require("../../md5.js");
    var tel = that.data.inputTelValue;
    console.log(that.data.inputTelValue);
    console.log(that.data.inputPwdValue);
      // 用户鉴权接口
    wx.request({
      url: 'https://papi.zcyun.cn/v1/auth',
      method: "POST",
      data: {
        account: that.data.inputTelValue,
        ts: timestamp,
        sign: md5.md5(md5.md5(that.data.inputPwdValue) + timestamp),
        platformId: app.globalData.platformId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data);
        if (res.data.code == "0") {
          app.globalData.isLogin = true;
          app.globalData.openId = res.data.openId;
          console.log("openId:" + res.data.openId);
          app.globalData.openKey = res.data.openKey;
          app.globalData.oldPassword = md5.md5(that.data.inputPwdValue);
          wx.setStorage({
            key: 'openId',
            data: res.data.openId,
            success: function (res) {
              console.log('异步保存成功')
            }
          })
          wx.setStorage({
            key: 'openKey',
            data: res.data.openKey,
            success: function (res) {
              console.log('异步保存成功')
            }
          })
          wx.setStorage({
            key: 'isLogin',
            data: true,
            success: function (res) {
              console.log('异步保存成功')
            }
          })
          wx.switchTab({
            url: '../../index/index'
          })
        } else {
          console.log(tel);
          wx.setStorage({
            key: 'isLogin',
            data: false,
            success: function (res) {
              console.log('异步保存成功')
            }
          })
          wx.request({
            url: 'https://papi.zcyun.cn/v1/user/valid?value=' + tel + '&platformId=' + app.globalData.platformId + '&type=telephone',
            success: function (res) {
              console.log(res.data);
              if (res.data.code == "20039") {
                wx.showToast({
                  title: '用户名或密码错误',
                  icon: "none",
                  duration: 2000
                })
                return;
              } else if (res.data.code == "0") {
                wx.showToast({
                  title: '请先注册',
                  icon: "none",
                  duration: 2000
                })
                return;
              }
            },
            fail: function (res) {
            }
          })
        }
      }
    })
  },
  bindRegisterBtn:function(){
    wx.navigateTo({
      url: '../userRegister/userRegister?isRegister=0',
    })
  },
  forgetPwdBind:function(){
    wx.navigateTo({
      url: '../userRegister/userRegister?isRegister=1',
    })
  }
})